<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: ConfigDatabase.php 323 2008-12-01 18:32:53Z steen.rabol@gmail.com $
 *
 */

class ConfigDatabase extends PortalAdminPage
{
	const CONFIG_FILE = 'Application.Data.Settings';

	public function onLoad($param)
	{
		parent::onLoad($param);

		$this->setAdminPageTitle('Database configuration');

		if($this->IsPostBack)
		{
			return;
		}

		$parameters = $this->Application->Parameters;

		// Database information
		$this->DBHost->Text		= $parameters['DbHost'];
		$this->DBDatabase->Text	= $parameters['DbDatabase'];
		$this->DBUser->Text		= $parameters['DbUser'];
		$this->DBPassword->Text	= $parameters['DbPassword'];
	}

	public function UpdateParameter(&$dom,$id,$value)
	{
		$parameters = &$dom->getElementsByTagName('parameter');
		foreach ($parameters as $param)
		{
			if($id == $param->getAttribute("id"))
			{
				$param->setAttribute('value',$value);
				return true;
			}
		}

		return false;
	}

	public function saveButtonClicked($sender, $param)
	{
		$dom = new TXmlDocument;
		if($dom->loadFromFile(Prado::getPathOfNamespace(self::CONFIG_FILE, '.xml')))
		{
			if(!$this->UpdateParameter($dom,'DbType',"MySQL"))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('DbType', "MySQL");
			}

			if(!$this->UpdateParameter($dom,'DbHost',$this->DBHost->Text))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('DbHost', $this->DBHost->Text);
			}

			if(!$this->UpdateParameter($dom,'DbDatabase',$this->DBDatabase->Text))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('DbDatabase', $this->DBDatabase->Text);
			}

			if(!$this->UpdateParameter($dom,'DbUser',$this->DBUser->Text))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('DbUser', $this->DBUser->Text);
			}

			if(!$this->UpdateParameter($dom,'DbPassword',$this->DBPassword->Text))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('DbPassword', $this->DbPassword->Text);
			}

			$dom->saveToFile(Prado::getPathOfNamespace(self::CONFIG_FILE, '.xml'));
		}
		$this->gotoDefaultPage();
	}

	private function createParameter($id, $value)
	{
		$element = new TXmlElement('parameter');
		$element->Attributes['id'] = $id;
		$element->Attributes['value'] = TPropertyValue::ensureString($value);
		return $element;
	}
}

?>